package com.egd.imapp.contoller.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.egd.imapp.R;
import com.egd.imapp.model.bean.InvitationInfo;
import com.egd.imapp.model.bean.UserInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 邀请信息列表页面的适配器
 */
public class InviteAdapter extends BaseAdapter {
    private Context mContext;
    private List<InvitationInfo> mInvitationInfos = new ArrayList<>();
    private OnInviteListener mOnInviteListener;
    private InvitationInfo invitationInfo;
    private String TAG = "TAG";

    public InviteAdapter(Context context, OnInviteListener onInviteListener) {
        mContext = context;
        mOnInviteListener = onInviteListener;
    }

    public void refreshInfo(List<InvitationInfo> invitationInfos){

        if (invitationInfos != null && invitationInfos.size() >= 0){
            mInvitationInfos.clear();

            mInvitationInfos.addAll(invitationInfos);

            notifyDataSetChanged();
        }
    }


    @Override
    public int getCount() {
        return mInvitationInfos == null ? 0 : mInvitationInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return mInvitationInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 获取或创建ViewHolder
        ViewHodler hodler = null;

        if (convertView == null){
            hodler = new ViewHodler();

            convertView = View.inflate(mContext, R.layout.item_invite, null);

            hodler.avater = convertView.findViewById(R.id.iv_invite__avater);

            hodler.name = convertView.findViewById(R.id.tv_invite_username);
            hodler.reason = convertView.findViewById(R.id.tv_invite_reason);

            hodler.accept = convertView.findViewById(R.id.btn_invite_accept);
            hodler.refuse = convertView.findViewById(R.id.btn_invite_refuse);

            hodler.alreadyAccept = convertView.findViewById(R.id.tv_invite_accept);
            hodler.alreadyRefuse = convertView.findViewById(R.id.tv_invite_refuse);

            convertView.setTag(hodler);
        }else {
            hodler = (ViewHodler) convertView.getTag();
        }

        // 获取当前item 数据
        invitationInfo = mInvitationInfos.get(position);

        switch (invitationInfo.getStatus()){
            case NEW_INVITE:
                hodler.alreadyAccept.setVisibility(View.GONE);
                hodler.alreadyRefuse.setVisibility(View.GONE);
                break;

            case INVITE_ACCEPT:
                hodler.alreadyAccept.setVisibility(View.VISIBLE);
                Log.e(TAG, "INVITE_ACCEPT: 邀请被接受，显示alreadyAccept");
                break;

            case INVITE_REJECT:
                hodler.alreadyRefuse.setVisibility(View.VISIBLE);
                Log.e(TAG, "INVITE_REJECT: 邀请被拒绝，显示alreadyRefuse");
                break;
            case GROUP_ACCEPT_INVITE:
                hodler.alreadyAccept.setVisibility(View.VISIBLE);
                Log.e(TAG, "GROUP_ACCEPT_INVITE: 群邀请被接受，alreadyAccept");
                break;
            case GROUP_REJECT_INVITE:
                hodler.alreadyRefuse.setVisibility(View.VISIBLE);
                Log.e(TAG, "GROUP_REJECT_INVITE: 群邀请被拒绝，显示alreadyRefuse");
                break;
            case GROUP_ACCEPT_APPLICATION:
                hodler.alreadyAccept.setVisibility(View.VISIBLE);
                Log.e(TAG, "GROUP_ACCEPT_APPLICATION: 申请入群被接受，alreadyAccept");
                break;
            case GROUP_REJECT_APPLICATION:
                hodler.alreadyRefuse.setVisibility(View.VISIBLE);
                Log.e(TAG, "GROUP_REJECT_APPLICATION: 申请入群被拒绝，alreadyRefuse");
                break;

            default:
                break;
        }

        // 显示当前item 数据
        UserInfo user = invitationInfo.getUser();

        if (user != null){ // 联系人
            hodler.name.setText(invitationInfo.getUser().getName());

            hodler.accept.setVisibility(View.GONE);
            hodler.refuse.setVisibility(View.GONE);


            if (invitationInfo.getStatus() == InvitationInfo.InvitationStatus.NEW_INVITE){
                // 新的邀请
                if (invitationInfo.getReason() == null){
                    hodler.reason.setText("请求添加好友");
                }else {
                    hodler.reason.setText(invitationInfo.getReason());
                }

                hodler.accept.setVisibility(View.VISIBLE);
                hodler.refuse.setVisibility(View.VISIBLE);

            } else if (invitationInfo.getStatus() == InvitationInfo.InvitationStatus.INVITE_ACCEPT){
                // 接受邀请
                if (invitationInfo.getReason() == null){
                    hodler.reason.setText(" 接受了你的邀请");
                }else {
                    hodler.reason.setText(invitationInfo.getReason());
                }

            } else if (invitationInfo.getStatus() == InvitationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER){
                // 邀请被接受
                if (invitationInfo.getReason() == null){
                    hodler.reason.setText("邀请被接受");
                }else {
                    hodler.reason.setText(invitationInfo.getReason());
                }

            }else if (invitationInfo.getStatus() == InvitationInfo.InvitationStatus.INVITE_REJECT){
                // 邀请被拒绝
                if (invitationInfo.getReason() == null){
                    hodler.reason.setText(" 拒绝了你的邀请");
                }else {
                    hodler.reason.setText(invitationInfo.getReason());
                }

            }

            // 按钮的处理
//           final ViewHodler finalHodler = hodler;
            hodler.accept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    finalHodler.alreadyAccept.setVisibility(View.VISIBLE);
                    mOnInviteListener.onAccept(invitationInfo);
                }
            });

            hodler.refuse.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    finalHodler.alreadyRefuse.setVisibility(View.VISIBLE);
                    mOnInviteListener.onRefuse(invitationInfo);

                }
            });

        }else { // 群组
            // 显示名称
            hodler.name.setText(invitationInfo.getGroup().getInvatePerson());

            hodler.accept.setVisibility(View.GONE);
            hodler.refuse.setVisibility(View.GONE);

            // 显示原因
            switch(invitationInfo.getStatus()){
                // 您的群申请请已经被接受
                case GROUP_APPLICATION_ACCEPTED:
                    hodler.reason.setText("您的群申请请已经被接受");
                    break;
                //  您的群邀请已经被接收
                case GROUP_INVITE_ACCEPTED:
                    hodler.reason.setText("您的群邀请已经被接收");
                    break;

                // 你的群申请已经被拒绝
                case GROUP_APPLICATION_DECLINED:
                    hodler.reason.setText("你的群申请已经被拒绝");
                    break;

                // 您的群邀请已经被拒绝
                case GROUP_INVITE_DECLINED:
                    hodler.reason.setText("您的群邀请已经被拒绝");
                    break;

                // 您收到了群邀请
                case NEW_GROUP_INVITE:
                    hodler.accept.setVisibility(View.VISIBLE);
                    hodler.refuse.setVisibility(View.VISIBLE);

                    // 接受邀请
                    hodler.accept.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnInviteListener.onInviteAccept(invitationInfo);
                        }
                    });

                    // 拒绝邀请
                    hodler.refuse.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnInviteListener.onInviteReject(invitationInfo);
                        }
                    });

                    hodler.reason.setText("您收到了群邀请");
                    break;

                // 您收到了群申请
                case NEW_GROUP_APPLICATION:
                    hodler.accept.setVisibility(View.VISIBLE);
                    hodler.refuse.setVisibility(View.VISIBLE);

                    // 接受申请
                    hodler.accept.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnInviteListener.onApplicationAccept(invitationInfo);
                        }
                    });

                    // 拒绝申请
                    hodler.refuse.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnInviteListener.onApplicationReject(invitationInfo);
                        }
                    });

                    hodler.reason.setText("您收到了群申请");
                    break;

                // 你接受了群邀请
                case GROUP_ACCEPT_INVITE:
                    hodler.reason.setText("你接受了群邀请");
                    break;

                // 您批准了群申请
                case GROUP_ACCEPT_APPLICATION:
                    hodler.reason.setText("您批准了群申请");
                    break;

                // 您拒绝了群邀请
                case GROUP_REJECT_INVITE:
                    hodler.reason.setText("您拒绝了群邀请");
                    break;

                // 您拒绝了群申请
                case GROUP_REJECT_APPLICATION:
                    hodler.reason.setText("您拒绝了群申请");
                    break;
            }

        }

        return convertView;
    }

    private class ViewHodler{
        private ImageView avater;

        private TextView name;
        private TextView reason;

        private Button accept;
        private Button refuse;

        private TextView alreadyAccept;
        private TextView alreadyRefuse;
    }

    public interface OnInviteListener{
        // 联系人被接受按钮的点击事件
        void onAccept(InvitationInfo invitationInfo);
        // 联系人被拒绝按钮的点击事件
        void onRefuse(InvitationInfo invitationInfo);

        // 接受邀请按钮处理
        void onInviteAccept(InvitationInfo invitationInfo);
        // 拒绝邀请按钮处理
        void onInviteReject(InvitationInfo invitationInfo);

        // 接受申请按钮处理
        void onApplicationAccept(InvitationInfo invitationInfo);
        // 拒绝申请按钮处理
        void onApplicationReject(InvitationInfo invitationInfo);
    }
}
